package a19_flyweight;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class FlyweightFactory {
	// 对象池
	private Map<Character, Flyweight> fwpool = new HashMap<Character, Flyweight>();
	private Lock lock = new ReentrantLock();

	public Flyweight getObject(Character state) {
		lock.lock();
		Flyweight fly = fwpool.get(state);
		if (fly == null) {
			fly = new ConcreteFlyweight(state);
			fwpool.put(state, fly);
		}
		lock.unlock();
		return fly;
	}
}
